package com.huawei.zyb.taskManager.interfaces.rest;

import com.huawei.zyb.taskManager.application.project.ProjectApplicationService;
import com.huawei.zyb.taskManager.application.project.dto.ProjectDTO;
import com.huawei.zyb.taskManager.domain.project.valueobject.ProjectStatus;
import com.huawei.zyb.taskManager.domain.shared.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 项目控制器
 */
@RestController
@RequestMapping("/api/projects")
public class ProjectController {
    
    private final ProjectApplicationService projectApplicationService;
    
    @Autowired
    public ProjectController(ProjectApplicationService projectApplicationService) {
        this.projectApplicationService = projectApplicationService;
    }
    
    /**
     * 创建项目请求对象
     */
    public static class CreateProjectRequest {
        @NotBlank(message = "项目名称不能为空")
        @Size(max = 200, message = "项目名称长度不能超过200个字符")
        private String name;
        
        @Size(max = 2000, message = "项目描述长度不能超过2000个字符")
        private String description;
        
        private String managerId;
        
        // Getters and Setters
        public String getName() { return name; }
        public void setName(String name) { this.name = name; }
        public String getDescription() { return description; }
        public void setDescription(String description) { this.description = description; }
        public String getManagerId() { return managerId; }
        public void setManagerId(String managerId) { this.managerId = managerId; }
    }
    
    /**
     * 更新项目请求对象
     */
    public static class UpdateProjectRequest {
        @NotBlank(message = "项目名称不能为空")
        @Size(max = 200, message = "项目名称长度不能超过200个字符")
        private String name;
        
        @Size(max = 2000, message = "项目描述长度不能超过2000个字符")
        private String description;
        
        // Getters and Setters
        public String getName() { return name; }
        public void setName(String name) { this.name = name; }
        public String getDescription() { return description; }
        public void setDescription(String description) { this.description = description; }
    }
    
    /**
     * 创建项目
     */
    @PostMapping
    public ResponseEntity<?> createProject(@Valid @RequestBody CreateProjectRequest request) {
        try {
            ProjectDTO project = projectApplicationService.createProject(
                request.getName(),
                request.getDescription(),
                request.getManagerId()
            );
            return ResponseEntity.status(HttpStatus.CREATED).body(project);
        } catch (BusinessException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }
    
    /**
     * 根据ID查找项目
     */
    @GetMapping("/{id}")
    public ResponseEntity<?> findProjectById(@PathVariable Long id) {
        try {
            ProjectDTO project = projectApplicationService.findById(id);
            return ResponseEntity.ok(project);
        } catch (BusinessException e) {
            return ResponseEntity.notFound().build();
        }
    }
    
    /**
     * 更新项目信息
     */
    @PutMapping("/{id}")
    public ResponseEntity<?> updateProject(@PathVariable Long id, @Valid @RequestBody UpdateProjectRequest request) {
        try {
            projectApplicationService.updateProject(
                id,
                request.getName(),
                request.getDescription()
            );
            return ResponseEntity.ok().build();
        } catch (BusinessException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }
    
    /**
     * 删除项目
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<?> deleteProject(@PathVariable Long id) {
        try {
            projectApplicationService.deleteProject(id);
            return ResponseEntity.noContent().build();
        } catch (BusinessException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }
    
    /**
     * 分配项目经理
     */
    @PostMapping("/{id}/assign")
    public ResponseEntity<?> assignManager(@PathVariable Long id, @RequestParam String managerId) {
        try {
            projectApplicationService.assignManager(id, managerId);
            return ResponseEntity.ok().build();
        } catch (BusinessException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }
    
    /**
     * 开始项目
     */
    @PostMapping("/{id}/start")
    public ResponseEntity<?> startProject(@PathVariable Long id) {
        try {
            projectApplicationService.startProject(id);
            return ResponseEntity.ok().build();
        } catch (BusinessException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }
    
    /**
     * 暂停项目
     */
    @PostMapping("/{id}/pause")
    public ResponseEntity<?> pauseProject(@PathVariable Long id) {
        try {
            projectApplicationService.pauseProject(id);
            return ResponseEntity.ok().build();
        } catch (BusinessException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }
    
    /**
     * 恢复项目
     */
    @PostMapping("/{id}/resume")
    public ResponseEntity<?> resumeProject(@PathVariable Long id) {
        try {
            projectApplicationService.resumeProject(id);
            return ResponseEntity.ok().build();
        } catch (BusinessException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }
    
    /**
     * 完成项目
     */
    @PostMapping("/{id}/complete")
    public ResponseEntity<?> completeProject(@PathVariable Long id) {
        try {
            projectApplicationService.completeProject(id);
            return ResponseEntity.ok().build();
        } catch (BusinessException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }
    
    /**
     * 取消项目
     */
    @PostMapping("/{id}/cancel")
    public ResponseEntity<?> cancelProject(@PathVariable Long id) {
        try {
            projectApplicationService.cancelProject(id);
            return ResponseEntity.ok().build();
        } catch (BusinessException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }
    
    /**
     * 根据项目经理ID查找项目列表
     */
    @GetMapping("/manager/{managerId}")
    public ResponseEntity<List<ProjectDTO>> findProjectsByManagerId(@PathVariable String managerId) {
        List<ProjectDTO> projects = projectApplicationService.findByManagerId(managerId);
        return ResponseEntity.ok(projects);
    }
    
    /**
     * 根据状态查找项目列表
     */
    @GetMapping("/status/{status}")
    public ResponseEntity<List<ProjectDTO>> findProjectsByStatus(@PathVariable ProjectStatus status) {
        List<ProjectDTO> projects = projectApplicationService.findByStatus(status);
        return ResponseEntity.ok(projects);
    }
    
    /**
     * 查找所有项目
     */
    @GetMapping
    public ResponseEntity<List<ProjectDTO>> findAllProjects() {
        List<ProjectDTO> projects = projectApplicationService.findAll();
        return ResponseEntity.ok(projects);
    }
    
    /**
     * 根据名称模糊查询项目
     */
    @GetMapping("/search")
    public ResponseEntity<List<ProjectDTO>> findProjectsByNameLike(@RequestParam String name) {
        List<ProjectDTO> projects = projectApplicationService.findByNameLike(name);
        return ResponseEntity.ok(projects);
    }
    

}